package com.qlu.syq.service.admin.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.qlu.syq.aspectj.DataScope;
import com.qlu.syq.entity.RoomType;
import com.qlu.syq.enums.RoomStatus;
import com.qlu.syq.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qlu.syq.dao.admin.RoomDao;
import com.qlu.syq.entity.admin.Room;
import com.qlu.syq.service.admin.RoomService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoomServiceImpl implements RoomService {

	@Autowired
	private RoomDao roomDao;
	@Autowired
	private RoomTypeService roomTypeService;
	
	@Override
	public int add(Room room) {
		return roomDao.add(room);
	}

	@Override
	public int edit(Room room) {
		return roomDao.edit(room);
	}

	@Override
	public int delete(Long id) {
		return roomDao.delete(id);
	}

	@Override
	@DataScope(alias = "f")
	public List<Room> findList(Map<String, Object> queryMap) {
		return roomDao.findList(queryMap);
	}

	@Override
	@DataScope(alias = "f")
	public List<Room> findAll(Map<String, Object> queryMap) {
		return roomDao.findAll(queryMap);
	}

	@Override
	@DataScope(alias = "f")
	public Integer getTotal(Map<String, Object> queryMap) {
		return roomDao.getTotal(queryMap);
	}

	@Override
	public Room find(Long id) {
		
		return roomDao.find(id);
	}

	@Override
	public Room findBySn(String sn) {
		
		return roomDao.findBySn(sn);
	}

	@Override
	public List<Map> findRoomList(Map<String, Object> queryMap) {
		
		return roomDao.findRoomList(queryMap);
	}

	/**
	 * 添加房间
	 */
	@Override
	@Transactional
	public int addRoom(Room room) {
		/*
			1.查询roomType信息,对应类型 roomNum avilableNum +1
			2.插入room信息
		*/
		Long roomTypeId = room.getRoomTypeId();
		RoomType roomType = roomTypeService.find(roomTypeId);
		roomType.setRoomNum(roomType.getRoomNum() + 1);
		roomType.setAvilableNum(roomType.getAvilableNum() + 1);
		roomTypeService.updateNum(roomType);
		return roomDao.add(room);
	}

	/**
	 * 修改房间
	 */
	@Override
	@Transactional
	public int editRoom(Room room) {
		/*
			1.查询原roomType信息,对应类型
			2.与新roomType比较，不同则 原roomType roomNum avilableNum -1， 新roomType roomNum avilableNum +1
			2.修改room信息
		*/
		Room oldRoom = roomDao.find(room.getId());
		Long oldRoomRoomTypeId = oldRoom.getRoomTypeId();
		Long roomTypeId = room.getRoomTypeId();

		if (!oldRoomRoomTypeId.equals(roomTypeId)) {
			RoomType oldRoomType = roomTypeService.find(oldRoomRoomTypeId);
			oldRoomType.setRoomNum(oldRoomType.getRoomNum() - 1);
			oldRoomType.setAvilableNum(oldRoomType.getAvilableNum() - 1);
			roomTypeService.updateNum(oldRoomType);

			RoomType newRoomType = roomTypeService.find(roomTypeId);
			newRoomType.setRoomNum(newRoomType.getRoomNum() + 1);
			newRoomType.setAvilableNum(newRoomType.getAvilableNum() + 1);
			roomTypeService.updateNum(newRoomType);
		}
		return roomDao.edit(room);
	}

	@Override
	@Transactional
	public int deleteRoom(Long id) {
		/*
			1.查询roomType信息,对应类型 roomNum avilableNum -1
			2.删除room信息
		*/
		Room room = roomDao.find(id);
		Long roomTypeId = room.getRoomTypeId();
		RoomType roomType = roomTypeService.find(roomTypeId);
		roomType.setRoomNum(roomType.getRoomNum() - 1);
		roomType.setAvilableNum(roomType.getAvilableNum() - 1);
		roomTypeService.updateNum(roomType);

		return roomDao.delete(id);
	}

	/**
	 * 获取未使用的房间
	 * @param queryMap
	 * @return
	 */
	@Override
	@DataScope(alias = "f")
	public List<Room> listAllUnused(Map<String, Object> queryMap) {
		List<Room> unUsedList = new ArrayList<>();
		List<Room> list = roomDao.findList(queryMap);
		for (Room room : list) {
			if (room.getStatus() == RoomStatus.UNUSED) {
				unUsedList.add(room);
			}
		}
		return unUsedList;
	}
}
